<?php
/*本文件负责根据传入的信息，调用相关函数，实现登录、注册等功能状态的判断 */
session_start();
require_once './conn.php';

function ShowDialog($title, $content)
{
    //显示对话框
    echo <<<str
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, shrink-to-fit=no"/>
    <meta name="renderer" content="webkit"/>
    <meta name="force-rendering" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <title>学生信息管理系统</title>
    <link rel="icon" href="../src/img/favicon.ico">
    <link rel="shortcut icon" href="../src/favicon.ico">
    <link rel="stylesheet" href="../src/css/drxs.min.css"/>
</head>
<body class="mdui-theme-primary-blue mdui-theme-accent-blue" onload="{
        var inst = mdui.dialog({
        title: '$title',
        content: '$content',
        cssClass:'mdui-color-blue-50',
        buttons: [{text: '确认'} ]});
        inst.open();
        setTimeout(function(){inst.close();},2200);}">
<div class="mdui-container"></div>
<script src="../src/js/mdui.min.js"></script>
<script src="../src/js/drxs.min.js"></script>
</body>
</html>
str;
}

if (isset($_POST['login_btn'])) {
    //登录功能
    $login_email = $_POST['login_email'];
    $login_pwd = $_POST['login_pwd'];
    $code = 3;
    $code = UserLogin($login_email, $login_pwd);
    if ($code == 1) {
        //登录成功
        LoginInit($login_email);
        if (isset($_POST['auto_login_checkbox'])) {
            //设置Cookie、记录Token，以实现自动登录
            $token = 'sims_token_' . date('YmdHisaO') . '_' . uniqid('random', true) . md5(date('YmdHisaO')) . '_' . crypt($login_pwd, 'sims') . '_' . md5($login_email);
            InsertToken($login_email, $token);
            setcookie('Token', $token, time() + 30 * 24 * 60 * 60, '/');
        }
        //弹出对话框，提示登录成功
        ShowDialog("登录成功!", "账号密码验证成功，即将跳转至新页面！");
    } elseif ($code == 2) { //密码错误
        ShowDialog("登录失败!", "密码输入错误，请返回重新登录！");
    } else {    //账号不存在
        ShowDialog("登录失败!", "当前账号不存在，请注册账号或使用其他账号登录！");
    }
} elseif (isset($_POST['register_btn'])) {
    //注册功能
    $register_email = $_POST['register_email'];
    $register_pwd = $_POST['register_pwd'];
    $code = 3;
    //根据返回值判断注册状态：1表示电子邮件已存在，2表示注册成功，3表示出错
    $code = UserRegister($register_email, $register_pwd);
    if ($code == 1) {
        //账号已存在
        ShowDialog("账号已存在！", "当前电子邮件已存在，请直接登录或更换电子邮件注册！");
    } elseif ($code == 2) {
        //提示注册成功
        ShowDialog("注册成功！", "恭喜你，注册成功，请牢记当前的电子邮件及密码！");
    } else {
        //数据添加失败
        ShowDialog("系统错误！", "抱歉，数据添加出错，请返回重试！");
    }
} elseif (isset($_GET['action']) && $_GET['action'] == 'logout') {
    //退出账号
    //清理Cookie
    setcookie('Token', '', time() - 1, '/');
    //清理session
    $_SESSION = array();
    if (session_destroy()) {
        ShowDialog("账号已退出登录！", "你已退出当前账号，如需继续访问，请重新登录！");
    } else {
        ShowDialog("系统错误！", "抱歉，系统内部错误，请返回重试！");
    }
} elseif (isset($_POST['change_info'])) {
    //修改个人信息功能
    $value = $_POST['value'];
    switch ($_POST['change_info']) {
        case '用户姓名':    //修改用户姓名
        {
            if (strlen($value) > 30)
                echo '抱歉，当前输入的数据长度过长，请检查后重试！';
            else {
                if (ChangeName($_SESSION['uid'], $value)) echo '恭喜，用户姓名已成功修改为：' . $value . '！';
                else echo '抱歉，用户姓名修改失败，请返回重试！';
            }
            break;
        }
        case '电子邮件':    //修改电子邮件
        {
            if (GetInfoByEmail($value))
                echo '抱歉，当前电子邮件已存在，请更换其他电子邮件！';
            elseif (strlen($value) > 40)
                echo '抱歉，当前电子邮件长度过长，请检查后重试';
            else {
                if (ChangeEmail($_SESSION['uid'], $value)) echo '恭喜，电子邮件已成功修改为：' . $value . '！';
                else echo '抱歉，电子邮件修改失败，请返回重试！';
            }
            break;
        }
        case '个性签名':    //修改个性签名
        {
            if (strlen($value) > 300)
                echo '个性签名长度错误，请检查后重试！';
            else {
                if (ChangeSign($_SESSION['uid'], $value)) echo '恭喜，个性签名已成功修改为：' . $value . '！';
                else echo '抱歉，个性签名修改失败，请返回重试！';
            }
            break;
        }
        case '年龄':    //修改年龄
        {
            if (strlen($value) > 3)
                echo '抱歉，年龄输入有误，请检查后重试!';
            else {
                if (ChangeAge($_SESSION['uid'], $value)) echo '恭喜，年龄已成功修改为：' . $value . '岁！';
                else echo '抱歉，年龄修改失败，请返回重试！';
            }
            break;
        }
        case '性别':    //修改性别
        {
            if (strlen($value) > 3)
                echo '抱歉，输入的数据长度错误，请检查后重试！';
            else {
                if (ChangeSex($_SESSION['uid'], $value)) echo '恭喜，性别已成功修改为：' . $value . '！';
                else echo '抱歉，性别修改失败，请返回重试！';
            }
            break;
        }
        case '电话':    //修改电话
        {
            if (strlen($value) > 11)
                echo '电话号码长度错误，请重新检查后再试！';
            else {
                if (ChangeTel($_SESSION['uid'], $value)) echo '恭喜，电话已成功修改为：' . $value . '！';
                else echo '抱歉，电话修改失败，请返回重试！';
            }
            break;
        }
        case '密码验证':    //验证输入的原始密码是否正确
        {
            if (strlen($value) < 4 || strlen($value) > 20)
                echo '密码长度错误！';
            else {
                if (UserLogin(GetInfoById($_SESSION['uid'])['Email'], $value) == 1) echo '验证通过';
                else echo '密码错误！';
            }
            break;
        }
        case '密码':    //修改密码
        {
            if (strlen($value) < 4 || strlen($value) > 20)
                echo '密码长度错误，请重新检查后再试！';
            else {
                if (ChangePassword($_SESSION['uid'], $value)) echo '恭喜，密码已修改成功！';
                else echo '抱歉，密码修改失败，请返回重试！';
            }
            break;
        }
        case '主题色':    //修改主题色
        {
            if (ChangeTheme($_SESSION['uid'], $value)) {
                //清理session，应用主题色修改
                unset($_SESSION['theme_color']);
                $style = GetUstyle($_SESSION['uid']);
                $_SESSION['theme_color'] = $style['ThemeColor'];
                echo '恭喜，主题色已修改成功！';
            } else echo '抱歉，主题色修改失败，请返回重试！';
            break;
        }
        case '主题强调色':    //修改主题强调色
        {
            if (ChangeAccent($_SESSION['uid'], $value)) {
                //清理session，应用主题色修改
                unset($_SESSION['theme_accent_color']);
                $style = GetUstyle($_SESSION['uid']);
                $_SESSION['theme_accent_color'] = $style['ThemeAccentColor'];
                echo '恭喜，主题强调色已修改成功！';
            } else echo '抱歉，主题强调色修改失败，请返回重试！';
            break;
        }
        case '表格配色':    //修改表格配色
        {
            $val_1 = $val_2 = $val_3 = '';
            switch ($value) {
                case 'setting_theme_table_1':
                {
                    $val_1 = $val_2 = $val_3 = 'mdui-color-white';
                    break;
                }
                case 'setting_theme_table_2':
                {
                    $val_1 = 'mdui-color-blue-100';
                    $val_2 = 'mdui-color-blue-50';
                    $val_3 = 'mdui-color-indigo-50';
                    break;
                }
                case 'setting_theme_table_3':
                {
                    $val_1 = 'mdui-color-blue-100';
                    $val_2 = 'mdui-color-cyan-50';
                    $val_3 = 'mdui-color-light-blue-50';
                    break;
                }
                case 'setting_theme_table_4':
                {
                    $val_1 = 'mdui-color-red-100';
                    $val_2 = 'mdui-color-pink-50';
                    $val_3 = 'mdui-color-purple-50';
                    break;
                }
                case 'setting_theme_table_5':
                {
                    $val_1 = 'mdui-color-light-blue-100';
                    $val_2 = 'mdui-color-cyan-50';
                    $val_3 = 'mdui-color-teal-50';
                    break;
                }
                case 'setting_theme_table_6':
                {
                    $val_1 = 'mdui-color-amber-50';
                    $val_2 = 'mdui-color-lime-50';
                    $val_3 = 'mdui-color-light-green-50';
                    break;
                }
            }
            if (ChangeTableColor($_SESSION['uid'], $val_1, $val_2, $val_3)) {
                //清理session，应用表格配色修改
                unset($_SESSION['table_color_1']);
                unset($_SESSION['table_color_2']);
                unset($_SESSION['table_color_3']);
                $style = GetUstyle($_SESSION['uid']);
                $_SESSION['table_color_1'] = $style['TableColor1'];
                $_SESSION['table_color_2'] = $style['TableColor2'];
                $_SESSION['table_color_3'] = $style['TableColor3'];
                echo '恭喜，表格配色已修改成功！';
            } else echo '抱歉，表格配色修改失败，请返回重试！';
            break;
        }
    }
} elseif (isset($_POST['get_theme'])) {
    //获取并返回当前颜色配置
    switch ($_POST['get_theme']) {
        case '主题色':
        {
            echo GetUstyle($_SESSION['uid'])['ThemeColor'];
            break;
        }
        case '主题强调色':
        {
            echo GetUstyle($_SESSION['uid'])['ThemeAccentColor'];
            break;
        }
        case '表格配色':
        {
            $val[0] = GetUstyle($_SESSION['uid'])['TableColor1'];
            $val[1] = GetUstyle($_SESSION['uid'])['TableColor2'];
            if ($val[0] == 'mdui-color-white')
                echo 'setting_theme_table_1';
            elseif ($val[0] == 'mdui-color-blue-100' && $val[1] == 'mdui-color-blue-50')
                echo 'setting_theme_table_2';
            elseif ($val[0] == 'mdui-color-blue-100' && $val[1] == 'mdui-color-cyan-50')
                echo 'setting_theme_table_3';
            elseif ($val[0] == 'mdui-color-red-100')
                echo 'setting_theme_table_4';
            elseif ($val[0] == 'mdui-color-light-blue-100')
                echo 'setting_theme_table_5';
            else
                echo 'setting_theme_table_6';
        }
    }
} elseif (isset($_POST['add_course_btn'])) {
    //添加课程
    $code = AddCourse($_POST['name'], $_POST['teacher'], $_POST['classroom'], $_POST['hour'], $_POST['credit'], $_POST['time']);
    if ($code == 1) echo '恭喜，添加成功！';
    else echo '抱歉，添加失败！';
} elseif (isset($_POST['add_score_btn'])) {
    //添加成绩
    $code = AddScore($_POST['course'], $_POST['student'], $_POST['score']);
    if ($code == 1) echo '恭喜，添加成功！';
    else echo '抱歉，添加失败！';
} elseif (isset($_POST['release_notice_btn'])) {
    //发布公告
    $code = ReleaseNotice($_POST['id'], $_POST['time'], $_POST['user'], $_POST['title'], $_POST['content']);
    if ($code == 1) echo '恭喜，公告发布成功！';
    else echo '抱歉，公告发布失败！';
} elseif (isset($_POST['init_db_table'])) {
    //初始化数据表
    $code = InitDb();
    echo $code;
} elseif (isset($_POST['install_db_data'])) {
    //导入示例数据
    InitDbData();
    echo '1';
} elseif (isset($_POST['delete_install_files'])) {
    //删除安装文件
    if (unlink('./drxs.sql') && unlink('./drxs_data.sql') &&
        unlink('./install_db.php'))
        echo '恭喜，安装文件已删除成功！';
} elseif (isset($_POST['delete_install_php'])) {
    //删除安装文件
    unlink('../install.php');
} elseif (isset($_POST['reset_token_time'])) {
    //更新自动登录有效时间
    $token = $_COOKIE['Token'];
    setcookie('Token', $token, time() + 30 * 24 * 60 * 60, '/');
} elseif (isset($_POST['auto_login_true'])) {
    //开启自动登录功能
    $token = 'sims_token_' . date('YmdHisaO') . '_' . uniqid('random', true) . md5(date('YmdHisaO')) . '_' . crypt($_SESSION['uid'], 'sims') . '_' . md5($_SESSION['uid']);
    setcookie('Token', $token, time() + 30 * 24 * 60 * 60, '/');
    InsertToken(GetInfoById($_SESSION['uid'])['Email'], $token);
} elseif (isset($_POST['auto_login_false'])) {
    //关闭自动登录功能
    setcookie('Token', '', time() - 1, '/');
} else {
    //直接访问此文件，提示非法访问。
    ShowDialog("非法访问！", "抱歉，请登录或注册账号！");
}

?>